package com.shujia.array;

import java.util.Arrays;

public class Homework3 {

    /*
    合并数组操作：现有如下一个数组：   int oldArr[]=
    {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}   
    要求将以上数组中值为0 的项去掉，将不为0的值存入一个新的数组，生成的新数组为：
     int newArr [] ={1,3,4,5,6,6,5,4,7,6,7,5}  
     */
    public static void main(String[] args) {
        int[] arr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
        int[] newArr = mergeArray(arr);
        System.out.println("merge:" + Arrays.toString(newArr));

    }

    // 排序的是数组
    public static int[] mergeArray(int[] arr) {
        int count = 0;
        for (int num : arr) {
            if (num == 0) {
                continue;
            }
            count++;
        }
        int[] newArr = new int[count];
        int index = 0;
        for (int num : arr) {
            if (num == 0) {
                continue;
            } else {
                newArr[index++] = num;
            }
        }

        return newArr;
    }
}
